package com.briup.MR.DefinePartitioner;

import org.apache.hadoop.conf.Configuration;
import org.apache.hadoop.conf.Configured;
import org.apache.hadoop.fs.Path;
import org.apache.hadoop.io.Text;
import org.apache.hadoop.mapreduce.Job;
import org.apache.hadoop.mapreduce.lib.input.KeyValueTextInputFormat;
import org.apache.hadoop.mapreduce.lib.output.TextOutputFormat;
import org.apache.hadoop.util.Tool;
import org.apache.hadoop.util.ToolRunner;

/*
 *采用默认的Map和reduce
处理器keyvalueTextInputFormat
yarn jar HdfsClientDemo-1.0-SNAPSHOT.jar com.briup.MR.DefinePartitioner.PartitionerMR -D input=/user/hdfs/phone.txt -D output=/user/hdfs/ph_result

 */
public class PartitionerMR extends Configured implements Tool {
    public int run(String[] strings) throws Exception {
        Configuration conf = getConf();
        String input = conf.get("input");
        String output = conf.get("output");
        conf.set("mapreduce.input.keyvaluelinerecordreader.key.value.separator",",");

        Job job = Job.getInstance(conf);
        job.setJarByClass(this.getClass());
        job.setJobName("PartitionerMR");

        job.setMapOutputKeyClass(Text.class);
        job.setMapOutputValueClass(Text.class);
        //设置分区规则
        job.setPartitionerClass(PhonePartitioner.class);
        job.setNumReduceTasks(3);



        job.setInputFormatClass(KeyValueTextInputFormat.class);
        KeyValueTextInputFormat.addInputPath(job,new Path(input));
        TextOutputFormat.setOutputPath(job,new Path(output));
        return job.waitForCompletion(true)?0:-1;
    }

    public static void main(String[] args) throws Exception {
        System.exit(new ToolRunner().run(new PartitionerMR(),args));
    }
}
